{include file="/public/header"}
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">内&emsp;容</label>
                        <div class="layui-input-inline">
                            <input name="text" class="layui-input" placeholder="输入ID或盗版内容"/>
                        </div>
                    </div>
                    <div class="layui-inline layui-btn-container">&emsp;&emsp;
                        <button class="layui-btn icon-btn" lay-filter="SF_TbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->

            <table id="SF_Table" lay-filter="SF_Table"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="SF_TbBar">
    <a class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-search"></i>查看</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="SF_EditDialog">
    <form id="SF_EditForm" lay-filter="SF_EditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div style="display: none">
            <textarea id="copy" name="copy"/></textarea>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">盗版内容:</label>
            <div class="layui-input-block">
                <input type="text" name="pirate_info" placeholder="无盗版内容" class="layui-input layui-disabled"
                       lay-verType="tips" disabled/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">IP:</label>
            <div class="layui-input-block">
                <input type="text" name="ip" placeholder="无IP" class="layui-input layui-disabled"
                       lay-verType="tips" disabled/>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">请求参数:</label>
            <div class="layui-input-block">
                <textarea name="param" class="layui-textarea layui-disabled" style="height:100px" placeholder="无参数" disabled></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">添加时间:</label>
            <div class="layui-input-block">
                <input type="datetime" name="addtime" placeholder="无添加时间" class="icon-date layui-input layui-disabled"
                       lay-verType="tips" disabled/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="SF_EditSubmit" lay-submit>复制</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
        </div>
    </form>
</script>
{include file="/public/footer"}
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'notice'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;
        var notice = layui.notice;

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#SF_Table',
            url: '{:url("/Pirate/list")}',
            method: 'post',
            page: true,
            skin: 'line',
            size: 'lg',
            cellMinWidth: 100,
            request: {
                pageName: 'current_page' //页码的参数名称，默认：page
                ,limitName: 'limit' //每页数据量的参数名，默认：limit
            },
            parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.data.total, //解析数据长度
                    "data": res.data.data.data //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox', fixed: true},
                {field: 'id', title: 'ID', sort: true, width: 80, fixed: true},
                {field: 'appName', title: '所属应用', sort: true, minWidth: 120, templet: function (d) {
                        return '<span class="layui-badge layui-badge-orange">'+d.appName+'</span>';
                    }},
                {field: 'pirate_info', title: '盗版内容', sort: true, minWidth: 160},
                {field: 'param', title: '请求参数', sort: true, minWidth: 120},
                {field: 'ip', title: 'IP', sort: true, minWidth: 100},
                {field: 'addtime', title: '添加时间', sort: true, minWidth: 180},
                {title: '操作', toolbar: '#SF_TbBar', align: 'center', minWidth: 180}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(SF_TbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(SF_Table)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            }
        });

        function copyText(text) {
            var textarea = document.createElement("textarea");//创建input对象
            var currentFocus = document.activeElement;//当前获得焦点的元素
            document.body.appendChild(textarea);//添加元素
            textarea.value = text;
            textarea.focus();
            if(textarea.setSelectionRange)
                textarea.setSelectionRange(0, textarea.value.length);//获取光标起始位置到结束位置
            else
                textarea.select();
            try {
                var flag = document.execCommand("copy");//执行复制
            } catch(eo) {
                var flag = false;
            }
            document.body.removeChild(textarea);//删除元素
            currentFocus.focus();
            return flag;
        }

        function showEditModel(mData) {
            layer.confirm('确定要花费 <span class="layui-badge layui-badge-yellow">{$price} 元</span> 查看该盗版授权的信息吗？', {
                skin: 'layui-layer-admin',
                icon: 3,
                shade: .1
            }, function (i) {
                layer.close(i);
                admin.popupRight({
                    type: 1,
                    title: '查看盗版信息',
                    content: $('#SF_EditDialog').html(),
                    success: function (layero, dIndex) {
                        // 回显表单数据
                        notice.msg('正在获取列表中..', {icon: 4, close: true});
                        $.ajax({
                            type: "POST",
                            url: '{:url("/Pirate/edit")}',
                            data :{'id':mData.id, 'appid':mData.appid},
                            dataType: "json",
                            success: function(data) {
                                notice.destroy();
                                if (data.code == 0) {
                                    notice.msg(data.msg, {icon: 1, audio:'1'});
                                    var content = '授权内容：'+data.data.data.pirate_info+'\n请求参数：'+(data.data.data.param?data.data.data.param:'无参数')+'\nIP：'+data.data.data.ip+'\n添加时间：'+data.data.data.addtime;
                                    $('#copy').val(content);
                                    form.render();
                                    form.val('SF_EditForm', data.data.data);
                                } else {
                                    notice.msg(data.msg, {icon: 2, audio:'1'});
                                }
                            },
                            error: function() {
                                notice.destroy();
                                notice.msg("服务器错误！", {icon: 2, audio:'1'});
                            }
                        });
                        // 表单提交事件
                        form.on('submit(SF_EditSubmit)', function (data) {
                            notice.msg('正在执行中..', {icon: 4, close: true});
                            console.log(data.field)
                            var ssrsss = data.field.copy//获取文本
                            var flag = copyText(ssrsss); //传递文本
                            notice.destroy();
                            notice.msg(flag ? "复制成功！" : "复制失败！", {icon: 1, audio:'1'});
                            return false;
                        });

                        // 禁止弹窗出现滚动条
                        $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    }
                });
            });
        }
    });
</script>